package com.starsoft.trade.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.starsoft.frame.base.service.UserService;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.trade.bean.TradeAcount;
import com.starsoft.trade.bean.TradeBill;
import com.starsoft.trade.service.AcountStatusService;
import com.starsoft.trade.service.TradeAcountService;
import com.starsoft.trade.service.TradeBillService;
import com.starsoft.trade.trader.TraderService;

@RestController
@RequestMapping(value = "/trade/bill")
public class TradeBillController {

	@Autowired
	private TradeBillService service;
	
	@Autowired
	private AcountStatusService acountStatusService;
	
	@Autowired
	private TraderService traderService;
	
	@Autowired
	private TradeAcountService tradeAcountService;
	
	@RequestMapping(value = "/query")
	public Map<String, Object> query(ServletRequest request) throws Exception {
		Integer acountId = UserService.getUserContext().getAcountId();
		PageObject po = WebUtil.getPageObject(request);
		String secId = request.getParameter("secId");
		String billType = request.getParameter("billType");
		Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
		Date endDate = WebUtil.getDate(request, "endDate", "yyyy-MM-dd");
		return service.pageQueryBills(po, acountId, secId, billType, beginDate, endDate);
	}
	
	@RequestMapping(value = "/save")
	public Map<String, Object> save(TradeBill bill, ServletRequest request) throws Exception {
		boolean autoFee = StringUtil.isTrue(request.getParameter("autoFee"));
		Integer acountId = UserService.getUserContext().getAcountId();
		bill.setAcountId(acountId);
		if(autoFee){
			service.saveOrUpdate(bill);
		} else {
			service.saveOrUpdateFeeMannul(bill);
		}
		acountStatusService.deleteStatusAfter(acountId, bill.getBillTime());
		return WebUtil.sucesseModel("保存成功！");
	}
	
	@RequestMapping(value = "/delete")
	public Map<String, Object> delete(TradeBill bill, ServletRequest request) {
		Integer acountId = UserService.getUserContext().getAcountId();
		List<String> billIds = WebUtil.getList(request, "billIds");
		if(!billIds.isEmpty()){
			Integer[] intBillIds = new Integer[billIds.size()];
			for(int i=0;i<billIds.size();i++){
				intBillIds[i] = Integer.parseInt(billIds.get(i));
			}
			Date smallestDate = service.getSmallestBillTime(acountId, intBillIds);
			if(smallestDate!=null){
				acountStatusService.deleteStatusAfter(bill.getAcountId(), smallestDate);
			}
			service.delete(intBillIds);
		} else {
			service.delete(bill);
			acountStatusService.deleteStatusAfter(bill.getAcountId(), bill.getBillTime());
		}
		return WebUtil.sucesseModel("删除成功！");
	}
	
	@RequestMapping(value="/upload")
	public Map<String, Object> datayesUpload(@RequestParam(value = "file") MultipartFile file) throws IOException{
		TradeAcount tradeAcount = tradeAcountService.get(UserService.getUserContext().getAcountId());
		List<TradeBill> bills = traderService.importBill(tradeAcount, file.getInputStream());
		return WebUtil.sucesseModel("上传成功:"+ bills.size());
	}
}
